<?php
/**
 * 《公共信息》实用类
 *
 * @author 孙晓晔
 * @version $Id: InfoUtil.class.php 636 2009-11-25 05:49:36Z sunxy $
 */
require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class FinanceUtil extends AdminUtil {
	
	public $type = array(
		'-' => '请选择',
		'1' => '固定资产',
		'2' => '宽带网络费',
		'3' => '行政杂费',
		'4' => '招待费',
		'5' => '出差费',
		'6' => '交通费',
		'7' => '餐费',
	
		'9' => '其它',
	);
	
	public $account = array(
		// '-' => '请选择',
		'1' => '现金',
		'2' => '招商银行',
		'3' => '兴业银行',
		'4' => '中国民生银行',
		'5' => '中国建设银行',
		'6' => '中信银行',
			
		'7' => '支付宝',
		'8' => '微信支付',

		'9' => '其它',
	);
	
	public $payment = array(
		'-' => '请选择',
		'1' => '现金',		
		'2' => '转帐',
		'3' => '支票',
		'4' => '电汇',
		
		'9' => '其它',
	);
	
	public $pay = array(
		'-' => '请选择',
		'1' => '行内转帐',
		'2' => '大额支付',
		'3' => '小额支付',
		'4' => '同城票交',
		'5' => '汇票',
			
		// '6' => '同城转帐',
		// '7' => '行内转帐',
		// '8' => '异地电汇',

		'9' => '其它',
	);
		
	function __construct() {
		$request['update'] = '$Date: 2009/03/03 07:24:42 $';
		$request['revision'] = '$Revision: 1.5 $';
			
		parent::__construct($request);
	}
	
	function pretreat($controller) {
		parent::pretreat($controller);
				
		$controller->assign_by_ref('type_option', $this->type);
		
		$controller->assign_by_ref('account_option', $this->account);
		$controller->assign_by_ref('payment_option', $this->payment);
		
		$controller->assign_by_ref('pay_option', $this->pay);
		
		$this->t = array_merge($this->t, $this->b);
	}
	
	public $b = array(
		'in'=>'请输入收入信息的流水号，如123',
		'in_item'=>'请输入收入明细的流水号，如123',
		'driver'=>'请输入司机姓名拼音首字母，然后选择并回车即可。如张三，输入zs',
	);

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "delete from " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 更新状态
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function ajax_exchange($controller, $request) {
		$code = $request['currency'];
		$data = $this->_exchange($code);
		echo $data;
	}
	
	function _bank($info, $info_id, $payment_x) {
		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();
		
		return $aa->_bank($info, $info_id, $payment_x);
	}
	
	/**
	 * TRF1:同城招行个人账户转账
	 * TRF2:同城招行单位账户转账
	 * TRF3:同城他行转账
	 * TRT1:异地招行系统内快速汇款
	 * TRT2:异地招行系统内普通汇款
	 * TRT3:异地他行汇款
	 * 
	 * 注：类型为TRF1, TRF2, TRT1, TRT2时，收款方开户行必须含有“招商银行”四个汉字。
	 * 
	 * @param $to
	 */
	function _transmit_type($to, $zone = "0755", $city = "深圳") {
		$bank = "招商银行";		
		
		if ($to['zone'] == $zone || preg_match("/$city/", $to['city'])) {
			if (preg_match("/$bank/", $to['bank'])) {
				if ($to['type'] == "1") {
					return "TRF1";
				} else {
					return "TRF2";
				}
			} else {
				return "TRF3";
			}
		} else {
			if (preg_match("/$bank/", $to['bank'])) {
				return "TRT2";
			} else {
				return "TRT3";
			}
		}
		
		return "";
	}
	
	function _drc($to, $my) {
		$bank = "招商银行";
		
		$ret = array('drc'=>'行内', 'city'=>'同城');
		
		if ($to['bank'] <> $my['bank']) {
			$ret['drc'] = '行外';
		}
	
		if ($to['zone'] <> $my['zone']) {
			$ret['city'] = '异地';
 		}
	
		return $ret;
	}
	
	function ajax_mybank($controller, $request) {		
		$data = $this->_ajax_bank('1', $_SESSION['id']);
		echo json_encode($data);
	}
	
	function ajax_bank1($controller, $request) {
		$q = $this->_input($request['q']);
		
		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();
		
		$data = $aa->_ajax_bank('1', $q);
		echo json_encode($data);
	}
	
	function ajax_bank($controller, $request) {
		$this->ajax_bank2($controller, $request);
	}
	
	function ajax_bank2($controller, $request) {
		$q = $this->_input($request['q']);
		
		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();
		
		$data = $aa->_ajax_bank('2', $q);
		echo json_encode($data);
	}
	
	function ajax_bank3($controller, $request) {
		$q = $this->_input($request['q']);
	
		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();
	
		$data = $aa->_ajax_bank('3', $q);
		echo json_encode($data);
	}
		
	function _update_linkman($info, $info_id, $payment_x) {
		$json = json_decode($payment_x);
			
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$contact = new Contact();
		$contact->_update($info, $info_id, $json);
	}
	
	function _update_bank($info, $info_id, $payment_x) {
		$json = json_decode($payment_x);
			
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		$bank->_update($info, $info_id, $json);
	}
	
	function _payment_x($emp) {
		require_once(dirname(__FILE__) . '/Bank.class.php');
		$aa = new Bank();
		
		$bank = $aa->_bank_primary("1", $emp);
		
		$ret['bank'] = $bank['bank'] . $bank['branch'];
		$ret['name'] = $bank['name'];
		$ret['code'] = $bank['code'];
		$ret['content'] = '';
		
		$ret = json_encode($ret);
		
		return addslashes($ret);
	}
	
	function _my_bank($emp, $service, $bank = '招商银行', $type = '1') {
		require_once(dirname(__FILE__) . '/../emp/BankMy.class.php');
		$aa = new BankMy();
		
		return $aa->_bank($emp, $service, $bank, $type);
	}
	
	function _t3($t2) {		
		if (strtotime($t2) < time()) {
			return 0;
		}
		
		return $this->date_diff('d', date("Y-m-d"), $t2);
	}	
}